<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Smarty Class
 */
 
require_once(APPPATH.'libraries/smarty/Smarty.class.php');

class CI_Smarty extends Smarty {
	public $CI;
	
	// holds data from looping block plugins
	private $perpetual_data; 


	function __construct ($email_parser = FALSE) {
		parent::__construct();
		
		$this->perpetual_data = array();
	}
	
	/**
	* Initialize
	*
	* @param boolean $email_parser If this is the template parser for outgoing emails, set to TRUE
	*							   This overrides certain features of the parser.
	*/
	function initialize ($ismobile = false) {
		// if these folders don't exist, we're probably not finished installing yet...
		// let's stop so we don't throw errors
		if (!file_exists(FCPATH . 'var/templates_compiled')) {
			return FALSE;
		}
		
		// store CI within Smarty's object
		$this->CI =& get_instance();
		
		// turn down error reporting (makes templates a lot cleaner)
		$this->error_reporting = E_ALL & ~E_NOTICE;
		
		// specify directories
		//if ($ismobile){
		//$this->setTemplateDir(FCPATH.'templates/mgame');
		//}else {
			$this->setTemplateDir(FCPATH.'templates/frontend');
		//}
		$this->setCompileDir(FCPATH . 'var/templates_compiled');
		$this->setCacheDir(FCPATH . 'var/cache');
		
		
		// plugin directories
		$this->addPluginsDir(APPPATH . 'plugins/smarty');
		
		
		// register the prefilter which parses {module_installed} tags
		$this->registerFilter('pre',array($this,'pre_filter'));
	
		// set global template variables
		$this->assign('APPPATH',APPPATH);
		$this->assign('BASEPATH',BASEPATH);
		$this->assign('FCPATH',FCPATH);
		
		// put settings into template variables
		$settings = $this->CI->config->config;
		$this->assign('current_url',current_url());
		$uri_segments = array();
			$count = 1;
			foreach ($this->CI->uri->segments as $segment) {
				$uri_segments[$count] = $segment;
				$count++;
			}
		$fetch_class=$this->CI->router->fetch_class();
		$fetch_module = $this->CI->router->fetch_module();
		$this->assign('module',$fetch_module);
		$this->assign('fetch_class',$fetch_class);
		$this->assign('uri_segment',$uri_segments);
		$this->assign('uri_segments',$uri_segments);
		$this->assign('setting', $settings);
		$this->assign('settings', $settings);
		
		
	}
	
	/**
	* Display
	*
	* @param string $template
	*/
	function display ($template, $cache_id = null, $compile_id = null, $parent = null) {
		// automatically add .thtml extension if it's not already there (and no extension is)
		if (strpos($template,'.') === FALSE) {
			$template .= '.tpl';
		}
		
		parent::display($template, $cache_id, $compile_id, $parent);
	}
	
	/**
	* Pre Filter
	*
	* Templates pass through this function
	*
	* @param string $template
	* @object $smarty
	*
	* @return string
	*/
	function pre_filter ($template, $smarty) {
		$template = preg_replace_callback ('/\{module_installed\s*?name="(.*?)"\}(.*?)\{\/module_installed\}/is', array($this, 'pre_filter_module_installed') , $template);
		
		return $template;
	}
	
}
